package com.shuoyue.ycgk.ui.questionbank.practice.paperset.questionitem;

import android.content.Context;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;

import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.entity.QuestionSimple;
import com.shuoyue.ycgk.entity.UploadEstimateSucInfo;
import com.shuoyue.ycgk.entity.UserAnswerPager;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.ui.questionbank.QuestionUtil;
import com.shuoyue.ycgk.ui.questionbank.UploadAnswerContract;
import com.shuoyue.ycgk.ui.questionbank.UserPaperQuestionContract;
import com.shuoyue.ycgk.ui.questionbank.base.BaseQuestionActivity;
import com.shuoyue.ycgk.ui.questionbank.practice.paperset.QuestionSetCountActivity;
import com.shuoyue.ycgk.utils.XToast;
import com.shuoyue.ycgk.views.dialog.AnswerCardDialog;
import com.shuoyue.ycgk.views.dialog.QuesSetBackDialog;
import com.shuoyue.ycgk.views.dialog.common.HintConfirmDialog;

import java.util.ArrayList;
import java.util.List;


/**
 * \
 * 考题
 */
public class DoPaperActivity extends BaseQuestionActivity
        implements UserPaperQuestionContract.View, UploadAnswerContract.View {
    UploadAnswerContract.Presenter uploadAnswerPresenter;
    boolean isUploadAnswer = false;


    //    long useTimeMul;//已用时毫秒数
    UserPaperQuestionContract.Presenter spePresenter;
    UserTestPaper userpaper;

    public boolean isHot = false;

    public static void start(Context source,
                             UserTestPaper pager) {
        source.startActivity(new Intent(source, DoPaperActivity.class)
                .putExtra("pager", pager)
        );
    }

    public static void startHot(Context source,
                                UserTestPaper pager) {
        source.startActivity(new Intent(source, DoPaperActivity.class)
                .putExtra("pager", pager)
                .putExtra("isHot", true)

        );
    }

    @Override
    protected void initView() {
        super.initView();
        userpaper = (UserTestPaper) getIntent().getSerializableExtra("pager");
        isHot = getIntent().getBooleanExtra("isHot", false);
        card.setVisibility(isHot ? View.GONE : View.VISIBLE);
        name.setText(userpaper.getName());
    }

    @Override
    public Class getClassT() {
        return DoPaperMateralFragment.class;
    }

    @Override
    public Class getClassK() {
        return DoPaperSimpleFragment.class;
    }

    @Override
    public void initData() {
        super.initData();
        uploadAnswerPresenter = new UploadAnswerContract.Presenter();
        uploadAnswerPresenter.attachView(this);
        spePresenter = new UserPaperQuestionContract.Presenter();
        spePresenter.attachView(this);
        spePresenter.getQuestions(userpaper.getMemberTestPaperId(), 3, userpaper.getType());
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (questionFragmentAdapter != null) {
            BaseMvpFragment fragment = questionFragmentAdapter.getCurrentFragment();
            if (fragment instanceof DoPaperMateralFragment) {
                ((DoPaperMateralFragment) fragment).gesture.onTouchEvent(event);
            }
            super.dispatchTouchEvent(event);
        }
        return false;
    }

    @Override
    public void lastOne() {
        showAnswerCard();
//        UserAnswerPager userAnswerPager = getUserAnswerPager(questionParents, userpaper.getMemberTestPaperId());
//        if (userAnswerPager == null) {
//            QuestionSetCountActivity.start(mContext, userpaper, userpaper.getName(),
//                    (ArrayList<QuestionParent>) questionParents);
//            super.finish();
//        } else {
//            userAnswerPager.setIsSubmit(1);
//            uploadAnswerPresenter.uploadAnswer(userAnswerPager, 3, false);
//        }
    }

    @Override
    public void back() {
        finish();
    }

    @Override
    public void finish() {
        //TODO 结束前提交信息

        if (SPUtils.getPrefBoolean(mContext, "isShowSetBackDialog", true)) {
            //没有提示过
            QuesSetBackDialog quesSetBackDialog = new QuesSetBackDialog(mContext);
            quesSetBackDialog.setOkLisenter(isChecked -> {
                UserAnswerPager userAnswerPager = QuestionUtil.getUserAnswerPager(questionParents, userpaper.getMemberTestPaperId(), userpaper.getType());
                if (userAnswerPager == null) {
                    super.finish();
                } else {
//                    userAnswerPager.setTime(useTimeMul / 1000);
                    userAnswerPager.setIsSubmit(0);
                    uploadAnswerPresenter.uploadAnswerNoView(userAnswerPager, 3, true);
                    super.finish();
                }
            });
            quesSetBackDialog.show();
        } else {


            if (isUploadAnswer) {
                super.finish();
            } else {
                HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext,
                        "提示",
                        "是否退出练习");
                hintConfirmDialog.setOkListener(v -> {
                    UserAnswerPager userAnswerPager = QuestionUtil.getUserAnswerPager(questionParents, userpaper.getMemberTestPaperId(), userpaper.getType());
                    if (userAnswerPager == null) {
                        super.finish();
                    } else {
//                    userAnswerPager.setTime(useTimeMul / 1000);
                        userAnswerPager.setIsSubmit(0);
                        uploadAnswerPresenter.uploadAnswer(userAnswerPager, 3, true);

                    }

                });
                hintConfirmDialog.show();
            }
        }
    }

    AnswerCardDialog answerCardDialog;


    @Override
    public void showAnswerCard() {
        if (answerCardDialog == null) {
            answerCardDialog = new AnswerCardDialog(mContext, questionParents);
            answerCardDialog.addSelectCall(new AnswerCardDialog.AnswerCardCallBack() {
                @Override
                public void select(QuestionSimple questionSimple) {
                    goDesPosition(questionSimple);
                }

                @Override
                public void submit() {
                    UserAnswerPager userAnswerPager = QuestionUtil.getUserAnswerPager(questionParents, userpaper.getMemberTestPaperId(), userpaper.getType());
                    if (userAnswerPager == null) {
                        XToast.toast(mContext, "没有答案可以提交");
                    } else {
                        userAnswerPager.setIsSubmit(1);
                        uploadAnswerPresenter.uploadAnswer(userAnswerPager, 3, false);
                    }
                }
            });
        }
        answerCardDialog.setButtonText("提交试卷");
        answerCardDialog.show();
    }

    @Override
    public void setQuestions(List<QuestionParent> specialPager) {
        setThisActData(specialPager, isHot);//时政热点直接显示答案
        QuestionSimple des = getLastDonePos();
        if (des != null) {
            goDesPosition(des);
        }
    }

    @Override
    public void uploadSuc(boolean isFinish) {
        isUploadAnswer = true;
        if (isFinish)
            super.finish();
        else {
            QuestionSetCountActivity.start(mContext, userpaper, userpaper.getName(),
                    (ArrayList<QuestionParent>) questionParents);
            DoPaperActivity.super.finish();
        }

    }

    @Override
    public void uploadEstimatteSuc(boolean b, UploadEstimateSucInfo uploadEstimateSucInfo, int submit) {

    }

    @Override
    public void uploadFail(boolean isFinish, int submit) {
        HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext,
                "提示",
                "答案提交失败,是否取消保存,直接退出");
        hintConfirmDialog.setOkListener(v -> {
            UserAnswerPager userAnswerPager = QuestionUtil.getUserAnswerPager(questionParents, userpaper.getMemberTestPaperId(), userpaper.getType());
            if (userAnswerPager == null) {
                super.finish();
            } else {
//                userAnswerPager.setTime(useTimeMul / 1000);
                userAnswerPager.setIsSubmit(0);
                uploadAnswerPresenter.uploadAnswer(userAnswerPager, 3, false);
            }
        });
        hintConfirmDialog.setCancelListener(v -> super.finish());
        hintConfirmDialog.setButtonText("直接退出", "重新提交");
        hintConfirmDialog.show();
    }
}
